/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.dm;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Properties;

public class Conexion {
    
    private Socket conexion;
    private String IpServer = "";
    private DataOutputStream salida;
    private DataInputStream entrada;

    public Socket getConexion() {
        return conexion;
    }

    public DataInputStream getEntrada() {
        return entrada;
    }

    public DataOutputStream getSalida() {
        return salida;
    }
    
    public void Conectar()
    {
        try {
            Properties p = new Properties(System.getProperties());
            p.load(this.getClass().getResourceAsStream("resources/Domino.properties"));
            IpServer = p.getProperty("ipserver");
            // crear Socket para realizar la conexión con el servidor
            
            String[] nu = IpServer.split("_");            
            IpServer = IpServer.replace("_", ".");            
            conexion = new Socket( InetAddress.getByName("localhost"), 7896 );
            //conexion = new Socket( InetAddress.getByAddress(IpServer,new byte[]{ Integer.valueOf(nu[0]).byteValue(),Integer.valueOf(nu[1]).byteValue(),Integer.valueOf(nu[2]).byteValue(),Integer.valueOf(nu[3]).byteValue()}), 7896 );

             // establecer flujo de salida para los objetos
             salida = new DataOutputStream( conexion.getOutputStream() );
             // establecer flujo de entrada para los objetos
             entrada = new DataInputStream( conexion.getInputStream() );
             
             System.out.println(" final");
      
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex) {
                Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    
   public void cerrarConexion()
   { 
      try {
         salida.close();
         entrada.close();
         conexion.close();
      }
      catch( IOException excepcionES ) {
         excepcionES.printStackTrace();
      }
   }
}
